package fr.pascal;

import java.io.File;
import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartUtilities;
import org.jfree.chart.JFreeChart;
import org.jfree.data.general.DefaultPieDataset;

@WebServlet("/camembert")
public class CamembertServlet extends HttpServlet {

	private static final long serialVersionUID = 1L;
	
	private final String R_IMAGES = "images";
	private final String F_IMAGE = "camembert.png";
	
	public CamembertServlet() {
		super();
	}

	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {

		// parametres d'entrée
		int total = Integer.parseInt(request.getParameter("total"));
		int rouge = Integer.parseInt(request.getParameter("rouge"));
		int bleu = Integer.parseInt(request.getParameter("bleu"));
		int vert = Integer.parseInt(request.getParameter("vert"));
		InputDataDto inputDataDto = new InputDataDto(total, rouge, bleu, vert);

		// calcul des parametres de sortie
		int prouge = calculPourcentage(total, rouge);
		int pbleu = calculPourcentage(total, bleu);
		int pvert = calculPourcentage(total, vert);
		int pjaune = 100 - (prouge + pbleu + pvert);
		ResultatsDto resultatsDto = new ResultatsDto(prouge, pbleu, pvert, pjaune);
		
		// creation du repertoire contenant les images
		String pathDir = getServletContext().getRealPath("/");
		pathDir += File.separatorChar + R_IMAGES;
		File fileDir = new File(pathDir);
		if (! fileDir.exists()) {
			fileDir.mkdir();
		}
		
		// creation de l'image
		String pathImage = pathDir + File.separatorChar + F_IMAGE;
		File file = new File(pathImage);
		makeImage(resultatsDto, file);
		String pathPage = R_IMAGES + File.separatorChar + F_IMAGE;
		resultatsDto.setPathImage(pathPage);
		
		// mise en session des données
		HttpSession httpSession = request.getSession(true);
		httpSession.setAttribute("resultatsDto", resultatsDto);
		httpSession.setAttribute("inputDataDto", inputDataDto);

		// affichage des donnees
		getServletContext().getRequestDispatcher("/testCamembert.jsp").forward(request, response);
	}

	private int calculPourcentage(int total, int valeur) {
		return (valeur * 100) / total;
	}

	private void makeImage(ResultatsDto resultatsDto, File file) {
		DefaultPieDataset pieDataset = new DefaultPieDataset();
		pieDataset.setValue("Rouge", new Integer(resultatsDto.getProuge()));
		pieDataset.setValue("Bleu", new Integer(resultatsDto.getPbleu()));
		pieDataset.setValue("Vert", new Integer(resultatsDto.getPvert()));
		pieDataset.setValue("Jaune", new Integer(resultatsDto.getPjaune()));
		JFreeChart pieChart = ChartFactory.createPieChart("Test camembert", pieDataset, true, true, true);
		try {
			ChartUtilities.saveChartAsPNG(file, pieChart, 400, 250);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}
